package com.huikeportal.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huikeportal.dao.ChainStRptApiMapper;
import com.huikeportal.model.AllIndexModel;
import com.huikeportal.model.ReportGraphData;
import com.huikeportal.model.ReportWeatherData;
import com.huikeportal.model.SiteAllInfo;
import com.huikeportal.service.ChainStRptApiService;
import com.huikeportal.service.FootfallGlobalService;


@Service("ChainStRptApiService")
public class ChainStRptApiServiceImpl implements ChainStRptApiService {
	
	@Autowired
	ChainStRptApiMapper chainStRptApiMapper;
	
	@Autowired
	FootfallGlobalService footfallGlobalService;
	
	public List<ReportGraphData> getSitesDataAnalysis(HashMap<String, Object> map)  throws Exception{
		List<ReportGraphData> resultList=new ArrayList<ReportGraphData>();
		
		String timeType="";
		if(map.get("timeType")!=null && !map.get("timeType").toString().equals("")){
			timeType=map.get("timeType").toString();
		}
		String timeType_table="";
		if(map.get("timeType_table")!=null && !map.get("timeType_table").toString().equals("")){
			timeType_table=map.get("timeType_table").toString();
		}
		String from="";
		if(map.get("startDate")!=null && !map.get("startDate").toString().equals("")){
			from=map.get("startDate").toString();
		}
		String to="";
		if(map.get("endDate")!=null && !map.get("endDate").toString().equals("")){
			to=map.get("endDate").toString();
		}
		String type="";
		if(map.get("type")!=null && !map.get("type").toString().equals("")){
			type=map.get("type").toString();
		}else{
			return null;
		}
		
		String siteId="";
		if(map.get("siteId")!=null && !map.get("siteId").toString().equals("")){
			siteId=map.get("siteId").toString();
		}else{
			return null;
		}
		
		//根据日周月年的不同需要的参数map
		HashMap<String, Object> pramMap1 = footfallGlobalService.getparam(timeType_table,timeType,from,to);

		pramMap1.put("siteid",siteId);//放入场所id
		
		String[] types = type.split(",");
		for(String ty : types){
			if(ty != null && !"".equals(ty)){
				 HashMap<String,Object> typeMap = footfallGlobalService.getCalret(Integer.parseInt(ty));
				 pramMap1.put("calret"+ty, typeMap.get("calret"));
				 pramMap1.put("ziduan"+ty, typeMap.get("ziduan"));
			}
		}
		if(pramMap1.get("timeType_table").equals("hour")){
			
			resultList = chainStRptApiMapper.selectGraphDataHour(pramMap1);	
	
		}else if(pramMap1.get("timeType_table").equals("day")){
			
			resultList = chainStRptApiMapper.selectGraphDataDay(pramMap1);	
		
		}else if(pramMap1.get("timeType_table").equals("week")){
			
			resultList = chainStRptApiMapper.selectGraphDataWeek(pramMap1);	
			
		}else if(pramMap1.get("timeType_table").equals("month")){
			
			resultList = chainStRptApiMapper.selectGraphDataMonth(pramMap1);	
		
		}else{
			resultList = chainStRptApiMapper.selectGraphDataYear(pramMap1);	
		}
		return resultList;
	}
	
	
	public List<ReportGraphData> getCountSitesDataAnalysis(HashMap<String, Object> map)  throws Exception{
		List<ReportGraphData> resultList=new ArrayList<ReportGraphData>();
		
		String timeType="";
		if(map.get("timeType")!=null && !map.get("timeType").toString().equals("")){
			timeType=map.get("timeType").toString();
		}
		String timeType_table="";
		if(map.get("timeType_table")!=null && !map.get("timeType_table").toString().equals("")){
			timeType_table=map.get("timeType_table").toString();
		}
		String from="";
		if(map.get("startDate")!=null && !map.get("startDate").toString().equals("")){
			from=map.get("startDate").toString();
		}
		String to="";
		if(map.get("endDate")!=null && !map.get("endDate").toString().equals("")){
			to=map.get("endDate").toString();
		}
		String type="";
		if(map.get("type")!=null && !map.get("type").toString().equals("")){
			type=map.get("type").toString();
		}else{
			return null;
		}
		
		String siteId="";
		if(map.get("siteId")!=null && !map.get("siteId").toString().equals("")){
			siteId=map.get("siteId").toString();
		}else{
			return null;
		}
		
		//根据日周月年的不同需要的参数map
		HashMap<String, Object> pramMap1 = footfallGlobalService.getparam(timeType_table,timeType,from,to);

		pramMap1.put("siteid",siteId);//放入场所id
		
		String[] types = type.split(",");
		for(String ty : types){
			if(ty != null && !"".equals(ty)){
				 HashMap<String,Object> typeMap = footfallGlobalService.getCalret(Integer.parseInt(ty));
				 pramMap1.put("calret"+ty, typeMap.get("calret"));
				 pramMap1.put("ziduan"+ty, typeMap.get("ziduan"));
			}
		}
		if(pramMap1.get("timeType_table").equals("hour")){
			
			resultList = chainStRptApiMapper.selectCountDataHour(pramMap1);	
	
		}else if(pramMap1.get("timeType_table").equals("day")){
			
			resultList = chainStRptApiMapper.selectCountDataDay(pramMap1);
			//和应该有的日期对比，如果该日期没有数据设置为空，添加到list
			List<String> listDate=(List<String>)pramMap1.get("Betweens");
			if(resultList.size()!=listDate.size()){
				for (int da=0;da<listDate.size();da++){
					Boolean flg=false;
					for(int rep=0;rep< resultList.size();rep++){
						if(listDate.get(da).equals(resultList.get(rep).getDatatime())){
							flg=true;
						}
					}
					if(flg){
						continue;
					}else{
					ReportGraphData reportGraphData=new ReportGraphData();
					reportGraphData.setDatatime(listDate.get(da));
					reportGraphData.init();
					resultList.add(da, reportGraphData);
					}
				}
			}
		
		}else if(pramMap1.get("timeType_table").equals("week")){
			
			resultList = chainStRptApiMapper.selectCountDataWeek(pramMap1);
			//和应该有的周对比，如果该周没有数据设置为空，添加到list
			List<String> listDate=(List<String>)pramMap1.get("Betweens");
			if(resultList.size()!=listDate.size()){
				for (int da=0;da<listDate.size();da++){
					Boolean flg=false;
					for(int rep=0;rep< resultList.size();rep++){
						if(listDate.get(da).equals(resultList.get(rep).getYear()+resultList.get(rep).getWeek())){
							flg=true;
						}
					}
					if(flg){
						continue;
					}else{
					ReportGraphData reportGraphData=new ReportGraphData();
					reportGraphData.setYear(listDate.get(da).substring(0,4));
					reportGraphData.setWeek(listDate.get(da).substring(4));
					reportGraphData.init();
					resultList.add(da, reportGraphData);
					}
				}
			}
			
		}else if(pramMap1.get("timeType_table").equals("month")){
			
			resultList = chainStRptApiMapper.selectCountDataMonth(pramMap1);
			//和应该有的月对比，如果该月没有数据设置为空，添加到list
			List<String> listDate=(List<String>)pramMap1.get("Betweens");
			if(resultList.size()!=listDate.size()){
				for (int da=0;da<listDate.size();da++){
					Boolean flg=false;
					for(int rep=0;rep< resultList.size();rep++){
						if(listDate.get(da).equals(resultList.get(rep).getYear()+resultList.get(rep).getMonth())){
							flg=true;
						}
					}
					if(flg){
						continue;
					}else{
					ReportGraphData reportGraphData=new ReportGraphData();
					reportGraphData.setYear(listDate.get(da).substring(0,4));
					reportGraphData.setMonth(listDate.get(da).substring(4));
					reportGraphData.init();
					resultList.add(da, reportGraphData);
					}
				}
			}
		}else{
			resultList = chainStRptApiMapper.selectCountDataYear(pramMap1);
			List<String> listDate=(List<String>)pramMap1.get("Betweens");
			if(resultList.size()!=listDate.size()){
				for (int da=0;da<listDate.size();da++){
					Boolean flg=false;
					for(int rep=0;rep< resultList.size();rep++){
						if(listDate.get(da).equals(resultList.get(rep).getYear())){
							flg=true;
						}
					}
					if(flg){
						continue;
					}else{
					ReportGraphData reportGraphData=new ReportGraphData();
					reportGraphData.setYear(listDate.get(da));
					reportGraphData.init();
					resultList.add(da, reportGraphData);
					}
				}
			}
		}
		return resultList;
	}
	
	/**
	 * 查询图表数据
	 * @param map
	 * @return List<ReportGraphData>
	 * @throws ParseException
	 */
	@SuppressWarnings("unchecked")
	public List<ReportWeatherData> getDateDtWithWeather(HashMap<String, Object> map)  throws Exception{
		List<ReportWeatherData> resultList=new ArrayList<ReportWeatherData>();
		
		String timeType="";
		if(map.get("timeType")!=null && !map.get("timeType").toString().equals("")){
			timeType=map.get("timeType").toString();
		}
		String timeType_table="";
		if(map.get("timeType_table")!=null && !map.get("timeType_table").toString().equals("")){
			timeType_table=map.get("timeType_table").toString();
		}
		String startDate="";
		String endDate="";
		String from="";
		if(map.get("startDate")!=null && !map.get("startDate").toString().equals("")){
			from=map.get("startDate").toString();
		}
		String to="";
		if(map.get("endDate")!=null && !map.get("endDate").toString().equals("")){
			to=map.get("endDate").toString();
		}
		String type="";
		if(map.get("type")!=null && !map.get("type").toString().equals("")){
			type=map.get("type").toString();
		}
		String siteId="";
		if(map.get("siteId")!=null && !map.get("siteId").toString().equals("")){
			siteId=map.get("siteId").toString();
		}
		
//		HashMap<String,Object> typeMap = footfallGlobalService.getCalret(Integer.parseInt(type));
		HashMap<String, Object> pramMap1 = getparam(timeType,from,to);
//		List<String> listDate = (List<String>)pramMap1.get("Betweens");
		List<HashMap<String, Object>> pramMapTemp = getTemppramMap(pramMap1);

		for(int i = 0;i<pramMapTemp.size();i++){
			HashMap<String, Object> pramMapchild = pramMapTemp.get(i);
			pramMapchild.put("siteid",siteId);//放入场所id
			
			String[] types = type.split(",");
			for(String ty : types){
				if(ty != null && !"".equals(ty)){
					 HashMap<String,Object> typeMap1 = footfallGlobalService.getCalret(Integer.parseInt(ty));
					 pramMapchild.put("calret"+ty, typeMap1.get("calret"));
					 pramMapchild.put("ziduan"+ty, typeMap1.get("ziduan"));
				}
			}
			
			List<String> listDate1 = (List<String>) pramMapchild.get("Betweens");
			String tableList = getSelectTables(listDate1);
			pramMapchild.put("table2", tableList);
			List<ReportWeatherData> resultListChild = new ArrayList<ReportWeatherData>();
			
			resultListChild = chainStRptApiMapper.selectWeatherDataGraph(pramMapchild);	
			
			resultList.addAll(resultListChild);
		}
		
		return resultList;
	}
	
	/**
	  * 得到数据起始结束时间、及时间点list
	  * @return HashMap<String, Object>
	 * @throws ParseException 
	  */
	public HashMap<String, Object> getparam(String timeType,String from,String to) throws ParseException{
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		HashMap<String, Object> resultMap = new HashMap<String, Object>();
	
		//根据timeType计算查询几天的数据
		if(timeType.equals("0")){
			Date dateFrom= sf.parse(from);
			Date dateTo= sf.parse(to);
			resultMap.put("startDate",sf.format(dateFrom));
			resultMap.put("endDate", sf.format(dateTo));
		}else if(timeType.equals("30")){
			Date date=new Date();
			String endDate=sf.format(date);
			date.setDate(date.getDate()-30);
			String startDate=sf.format(date);
			resultMap.put("startDate", startDate);
			resultMap.put("endDate", endDate);
		}else if(timeType.equals("60")){
			Date date=new Date();
			String endDate=sf.format(date);
			date.setDate(date.getDate()-60);
			String startDate=sf.format(date);
			resultMap.put("startDate", startDate);
			resultMap.put("endDate", endDate);
		}else if(timeType.equals("90")){
			Date date=new Date();
			String endDate=sf.format(date);
			date.setDate(date.getDate()-90);
			String startDate=sf.format(date);
			resultMap.put("startDate", startDate);
			resultMap.put("endDate", endDate);
		}
		//根据开始时间结束时间算出时间段
		List<String> listDate=footfallGlobalService.getDatesBetweenTwoDate(resultMap.get("startDate").toString(),resultMap.get("endDate").toString());
		resultMap.put("Betweens", listDate);
	
		return resultMap;
	}
	
	private List<HashMap<String, Object>> getTemppramMap(HashMap<String, Object> pramMap1){
		
		List<String> Betweens = (List<String>) pramMap1.get("Betweens");
		List<HashMap<String, Object>> pramMapTemp = new ArrayList<HashMap<String,Object>>();
		for(String date:Betweens){
			SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
			Date dateFrom;
			int year = 0;
			try {
				dateFrom = sf.parse(date);
				year = dateFrom.getYear();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			boolean ifAdd = true;
			int index = 0;
			for(int i = 0;i<pramMapTemp.size();i++){
				HashMap<String, Object> mapd = pramMapTemp.get(i);
				int	MaptempYear = Integer.parseInt(mapd.get("year").toString());
				if(year == MaptempYear){
					ifAdd = false;
					index = i; 
				}
			}
			if(ifAdd){//还没有年分组需要加入
				HashMap<String, Object> mapadd = new HashMap<String, Object>();
				mapadd.put("year", year);
				List<String> datAdd = new ArrayList<String>();
				datAdd.add(date);
				mapadd.put("Betweens", datAdd);
				pramMapTemp.add(mapadd);
			}else{//已经有年的分组不需要加入
				HashMap<String, Object> mapadd = pramMapTemp.get(index);
				List<String> datAdd = (List<String>) mapadd.get("Betweens");
				datAdd.add(date);
				mapadd.put("Betweens", datAdd);
			}
		}
		//给分组的对象加入开始时间结束时间
		for(int i1 = 0;i1<pramMapTemp.size();i1++){
			HashMap<String, Object> pramMapTempch = pramMapTemp.get(i1);
			List<String> datchAdd  = (List<String>) pramMapTempch.get("Betweens");
			String startDate1 =  datchAdd.get(0);
			String endDate1 =  datchAdd.get(datchAdd.size()-1);
			pramMapTempch.put("startDate", startDate1);
			pramMapTempch.put("endDate", endDate1);
		}
		return pramMapTemp;
		
	}
	
	/**
	 * 根据要查询的事件段获取需要的天气数据表，本年为settings_weather，上一年为2014年
	 * 表为settings_weather_2014，上一年为 YEAR 表就为settings_weather_+YEAR
	 * @param listDate
	 * @return
	 */
	private String getSelectTables(List<String> listDate){ 
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		String endDatestr = listDate.get(0);
		Date endDate=new Date();
		try {
			endDate=sf.parse(endDatestr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		Calendar today = Calendar.getInstance();
		today.setTime(new Date());
		Calendar endDay = Calendar.getInstance();
		endDay.setTime(endDate);
		
		int todayYear=today.get(Calendar.YEAR);
		int endYear=endDay.get(Calendar.YEAR);
		String endTable = "";
		if(endYear==todayYear){
			endTable="settings_weather";
		}else{
			endTable="settings_weather_"+endYear;
		}
		
		return endTable;

		
	}
	
	public List<AllIndexModel> getIndexInfoByIndexId(HashMap<String, Object> map)  throws Exception{
		
		List<AllIndexModel>  AllIndexModels = chainStRptApiMapper.getIndexInfoByIndexId(map);
		return AllIndexModels;
		
	}
	
	public ReportGraphData getSiteMaxDataOfIndex(HashMap<String , Object> paraMap){
		HashMap<String, Object> pramMap1 = new HashMap<String, Object>();
		
		String siteId="";
		if(paraMap.get("siteId")!=null && !"".equals(paraMap.get("siteId").toString())){
			siteId = paraMap.get("siteId").toString();
		}
		pramMap1.put("siteid",siteId);
		
		String type = "";
		if(paraMap.get("type")!=null && !"".equals(paraMap.get("type").toString())){
			type = paraMap.get("type").toString();
		}
		String[] types = type.split(",");
		for(String ty : types){
			if(ty != null && !"".equals(ty)){
				 HashMap<String,Object> typeMap = footfallGlobalService.getCalret(Integer.parseInt(ty));
				 pramMap1.put("calret"+ty, typeMap.get("calretD"));
				 pramMap1.put("ziduan"+ty, typeMap.get("ziduan"));
			}
		}
		ReportGraphData reportGraphData =chainStRptApiMapper.selectMaxSitesData(pramMap1);	
		if(reportGraphData == null){
			ReportGraphData reportGraphDatatemp=new ReportGraphData();
			reportGraphDatatemp.init();
			reportGraphData = reportGraphDatatemp;
		}
		return reportGraphData;
	}

}
